package org.example;

import org.example.dao.MyDao;
import org.example.dao.OrderDao;
import org.example.dao.UserDao;
import org.example.factory.OrderDaoFactory;
import org.example.factory.UserDaoFactory;
import org.example.service.MyService;
import org.example.service.TestService;
import org.example.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        System.out.println("Hello world!");

        //获取IOC容器
        ClassPathXmlApplicationContext cxt = new ClassPathXmlApplicationContext("spring.xml");
        // 容器关闭钩子,告诉系统，关闭虚拟机之前关闭容器，位置不定
        cxt.registerShutdownHook();
        //获取bean--单例模式
        //MyDao myDao = (MyDao) cxt.getBean("myDao");
        //myDao.get();

        MyService myService = (MyService) cxt.getBean("myService");
        MyService myService1 = (MyService) cxt.getBean("myService");
        myService.getMyDaoInfo();

        System.out.println(myService);
        System.out.println(myService1);

        //通过静态工厂创建bean
        OrderDao orderDao = OrderDaoFactory.getOrderDao();
        orderDao.get();

        //通过xml配置创建对象
        OrderDao orderDao1 = (OrderDao) cxt.getBean("orderDao");
        orderDao1.get();

        // 实例工厂的创建对象方式
        UserDaoFactory factory = new UserDaoFactory();
        UserDao userDao = factory.getUserDao();
        userDao.get();

        //通过xml配置创建对象
        UserDao userDao1 = (UserDao) cxt.getBean("userDao");
        userDao1.get();

        //bean生命周期管理
        UserService userService = (UserService) cxt.getBean("userService");
        userService.get();
        // 关闭容器，位置很重要
        //cxt.close();

        TestService testService = (TestService) cxt.getBean("testService");
        testService.get();

    }
}